package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.util;

import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.common.bean.ApplicationContextHelper;
import com.kingyun.gpsinspection.purificationservice.common.enums.OrganizationLevelEnum;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Department;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import com.kingyun.gpsinspection.purificationservice.facade.model.webservicesmodel.UasOrg;
import com.kingyun.gpsinspection.purificationservice.facade.model.webservicesmodel.UasOrgExt;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.organization.JsmOrganizationService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class OrganizationPool implements Runnable {
    private List<UasOrg> uoList;//组织信息
    private List<UasOrgExt> uoeList;//组织扩展信息
    private String type;
    private String controlType;//1.添加，2.修改，3.删除

    public void setUoList(List<UasOrg> uoList) {
        this.uoList = uoList;
    }

    public void setUoeList(List<UasOrgExt> uoeList) {
        this.uoeList = uoeList;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    @Override
    public void run() {
        JsmOrganizationService organizationService = (JsmOrganizationService) ApplicationContextHelper.getBean("organizationService");
        if (organizationService != null && StringUtils.isNotEmpty(controlType)) {
            List<Organization> paramList = new ArrayList<Organization>();
//            List<Station> stationList = new ArrayList<Station>();
//            List<Well> wellList = new ArrayList<Well>();
//            List<Pipeline> pipelineList = new ArrayList<Pipeline>();
            List<Department> departmentList = new ArrayList<Department>();
//            organizationService.selectOrgByName("");
            try {
                uasOrgExt(uoeList, paramList, controlType, departmentList);
                organization(uoList, paramList, departmentList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if ("1".equals(type)) {//处理组织信息
                if ("1".equals(controlType)) {//添加
                    organizationService.insertList(paramList, departmentList);
                } else if ("2".equals(controlType)) {//修改
                    organizationService.updateList(paramList, departmentList);
                } else if ("3".equals(controlType)) {//删除
                    organizationService.deleteList(paramList, departmentList);
                }
            } else if ("2".equals(type)) {//处理组织扩展信息
                List<String> strList = organizationService.selectByListId(paramList);
                if (strList != null) {
                    List<Organization> updateList = new ArrayList<Organization>();
                    for (Organization o : paramList) {
                        if (strList.contains(o.getOrgId())) {
                            updateList.add(o);
                        }
                    }
                    organizationService.updateList(updateList);
                }
            }
        }
    }

    /**
     * 处理组织扩展信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void uasOrgExt(List<UasOrgExt> list, List<Organization> paramList, String controlType, List<Department> departmentList) throws Exception {
        if (list != null && list.size() > 0) {
            if ("3".equals(controlType)) {
                for (UasOrgExt uoe : list) {
                    Organization param = new Organization();
                    param.setOrgId(uoe.getOrgId());
                    param.setOrgCode("");
                    param.setTel1("");
                    param.setAddress("");
                    param.setPostcode("");
                    param.setOrgType("");
                    param.setOrgLevel("");
                    param.setDescription("");
                    Date date = new Date();
                    param.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
                    param.setOperateId(uoe.getModifier());
                    paramList.add(param);
                }
            } else {
                for (UasOrgExt uoe : list) {
                    Organization param = new Organization();
                    BeanUtils.copyProperties(param, uoe);
                    param.setTel1(uoe.getPhoneNumber());
                    param.setAddress(uoe.getPostAddress());
                    param.setDescription(uoe.getMemo());
                    Date date = new Date();
                    param.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
                    param.setOperateId(uoe.getModifier());
                    paramList.add(param);
                }
            }
        }
    }

    /**
     * 处理组织信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void organization(List<UasOrg> list, List<Organization> paramList, List<Department> departmentList) throws Exception {
        if (list != null && list.size() > 0) {
            Organization param = null;
            Date date = new Date();
            if ("1".equals(controlType)) {
                for (UasOrg uo : list) {
                    if("计划财务部".equals(uo.getOrgName())||"川东北高含硫气田开发建设项目部".equals(uo.getOrgName())||"人事处（党委组织部）".equals(uo.getOrgName())||"作业区数字化管理平台系统".equals(uo.getOrgName())){
                        continue;
                    }
                    if (uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_GAS_FIELD.getDesc()) > -1) {//气矿
                        param = new Organization();
                        BeanUtils.copyProperties(param, uo);
                        param.setOrgId(uo.getOldId());
                        param.setParentOrgId("XNJV3M3kqvjkKjumoBm5Yup5562SW9Hz");
                        param.setCreatorId(uo.getUserId());
                        param.setCreatorId("");
                        param.setStatus(0);
                        param.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
                        param.setOrgLevelId(OrganizationLevelEnum.ORG_LEVEL_GAS_FIELD.getCode());
                        param.setOldId(uo.getOrgId());
                        paramList.add(param);
                    } else if (uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_WORK_SPACE.getDesc()) > -1) { // 作业区
//                    } else if (uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_WORK_SPACE.getDesc()) > -1 || uo.getOrgName().equals("磨溪项目部")) { // 作业区
                        param = new Organization();
                        BeanUtils.copyProperties(param, uo);
                        param.setOrgId(uo.getOldId());
                        String pOrgId = uo.getOrgParentId().length() > 32 ? uo.getOrgParentId().substring(0, 32) : uo.getOrgParentId();
                        param.setParentOrgId(pOrgId);
                        param.setCreatorId(uo.getUserId());
                        param.setStatus(0);
                        param.setCreateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
                        param.setOrgLevelId(OrganizationLevelEnum.ORG_LEVEL_WORK_SPACE.getCode());
                        param.setOldId(uo.getOrgId());
                        paramList.add(param);
                    } else {
                        //部门
                        Department d = new Department();
                        BeanUtils.copyProperties(d, uo);
                        String pOrgId = uo.getOrgParentId().length() > 32 ? uo.getOrgParentId().substring(0, 32) : uo.getOrgParentId();
                        d.setParentOrgId(pOrgId);
                        d.setDepId(uo.getOldId());
                        d.setOldId(uo.getOrgId());
                        d.setDepName(uo.getOrgName());
                        d.setState(0);
                        d.setCreateTime(date);
                        departmentList.add(d);
                    }
                }
            } else if ("2".equals(controlType)) {
                for (UasOrg uo : list) {
                    if (uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_GAS_FIELD.getDesc()) > -1) {//气矿
                        param = new Organization();
                        BeanUtils.copyProperties(param, uo);
                        param.setOrgId(uo.getOldId());
                        param.setParentOrgId("XNJV3M3kqvjkKjumoBm5Yup5562SW9Hz");
                        param.setCreatorId(uo.getUserId());
                        param.setCreatorId("");
                        param.setStatus(0);
                        param.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
                        param.setOrgLevelId(OrganizationLevelEnum.ORG_LEVEL_GAS_FIELD.getCode());
                        param.setOldId(uo.getOrgId());
                        paramList.add(param);
                    } else if (uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_WORK_SPACE.getDesc()) > -1) { // 作业区
                        param = new Organization();
                        BeanUtils.copyProperties(param, uo);
                        param.setOrgId(uo.getOldId());
                        String pOrgId = uo.getOrgParentId().length() > 32 ? uo.getOrgParentId().substring(0, 32) : uo.getOrgParentId();
                        param.setParentOrgId(pOrgId);
                        param.setCreatorId(uo.getUserId());
                        param.setStatus(0);
                        param.setOperateDT(DateUtil.DateToString(date, DateStyle.YYYY_MM_DD_HH_MM));
                        param.setOrgLevelId(OrganizationLevelEnum.ORG_LEVEL_WORK_SPACE.getCode());
                        param.setOldId(uo.getOrgId());
                        paramList.add(param);
                    } else {
                        //部门
                        Department d = new Department();
                        BeanUtils.copyProperties(d, uo);
                        String pOrgId = uo.getOrgParentId().length() > 32 ? uo.getOrgParentId().substring(0, 32) : uo.getOrgParentId();
                        d.setParentOrgId(pOrgId);
                        d.setDepId(uo.getOldId());
                        d.setDepName(uo.getOrgName());
                        d.setState(0);
                        d.setModifyTime(date);
                        departmentList.add(d);
                    }
                }
            } else {
                for (UasOrg uo : list) {
                    if (uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_GAS_FIELD.getDesc()) > -1 || uo.getOrgName().lastIndexOf(OrganizationLevelEnum.ORG_LEVEL_WORK_SPACE.getDesc()) > -1) {//气矿 和 作业区
                        param = new Organization();
                        param.setOrgId(uo.getOldId());
                        paramList.add(param);
                    } else {
                        //部门
                        Department d = new Department();
                        d.setDepId(uo.getOldId());
                        departmentList.add(d);
                    }
                }
            }
        }
    }

//    if(OrgLevelUtils.ORG_LEVEL_STATION.equals(uo.getOrgLevel())){
//                    //场站
//                    Station s = new Station();
//                    s.setStationId(uo.getOrgId());
//                    s.setStationName(uo.getOrgName());
////                    s.setStationType();
//                }else if(OrgLevelUtils.ORG_LEVEL_STATION.equals(uo.getOrgLevel())){
//                    //井
//                    Well w = new Well();
//
//                }else if(OrgLevelUtils.ORG_LEVEL_STATION.equals(uo.getOrgLevel())){
//                    //管线
//                    Pipeline p = new Pipeline();
//                }


    public static void main(String[] args) {
        String ss = "XNhvrQcrRHiz74HfmUIhM7haRndQg2Iq2017021314555405356";
        System.out.println(ss.substring(0, 32));
    }
}
